package com.huaxiafinance.management.service.impl;

import com.huaxiafinance.management.dto.request.OrgDto;
import com.huaxiafinance.management.mapper.OrgMapper;
import com.huaxiafinance.management.model.Org;
import com.huaxiafinance.management.model.OrgExample;
import com.huaxiafinance.management.model.OrgExample.Criteria;
import com.huaxiafinance.management.service.OrgUserService;
import com.huaxiafinance.management.util.MessageResult;
import com.huaxiafinance.management.util.ServiceCode;
import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service("orgUserService")
public class OrgUserServiceImpl implements OrgUserService {

	private static final Logger log = Logger.getLogger(OrgUserServiceImpl.class);

	@Autowired
	OrgMapper orgMapper;

	@Override
	public MessageResult newOrg(OrgDto dto) {
		MessageResult messageResult = new MessageResult();
		
		Org org = new Org(dto);

		OrgExample example = new OrgExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentOrgCodeEqualTo(org.getOrgCode());
		example.setOrderByClause("org_code desc");
		
		List<Org> orgList = orgMapper.selectByExample(example);
		
		if (CollectionUtils.isNotEmpty(orgList)) {
			int orgCode = Integer.parseInt(orgList.get(0).getOrgCode()) + 1;
			org.setOrgCode("00" + orgCode);
		} else {
			if (dto.getOrgCode().equals("0")) {
				org.setOrgCode(dto.getOrgCode() + "01");
			} else {
				org.setOrgCode(dto.getOrgCode() + "001");
			}			
		}

		if (org.getOrgCode().length() > 12) {
			messageResult.setResultMessage(ServiceCode.ORG_CODE_TOO_LONG);
			return messageResult;
		}
		
		org.setParentOrgCode(dto.getOrgCode());
		
		int i = 0;

		try {
			i = orgMapper.insert(org);
		} catch (DuplicateKeyException e) {
			messageResult.setResultMessage(ServiceCode.DICT_CODE_DUPLICATE);
			log.error(ServiceCode.DICT_CODE_DUPLICATE);
		} catch (Exception e) {
			messageResult.setResultMessage(ServiceCode.EXCEPTION_ERROR);
			log.error(e.getMessage(), e);
		}

		if (i > 0)
			messageResult.setResultMessage(ServiceCode.SUCCESS);

		return messageResult;
	}
	
	@Transactional
	@Override
	public MessageResult orgIsEnable(OrgDto dto) {
		MessageResult messageResult = new MessageResult();
		int i = 0;

		try {

			Org org = new Org();
			org.setIsEnable(dto.getIsEnable());
			org.setUpdatedAt(new Date());
			OrgExample example = new OrgExample();
			example.createCriteria().andOrgCodeLike(dto.getOrgCode() + "%");
			
			i = orgMapper.updateByExampleSelective(org, example);
		} catch (Exception e) {
			messageResult.setResultMessage(ServiceCode.EXCEPTION_ERROR);
			log.error(e.getMessage(), e);
		}

		if (i > 0)
			messageResult.setResultMessage(ServiceCode.SUCCESS);

		return messageResult;
	}
	
	@Override
	public MessageResult updOrg(OrgDto dto) {
		Org org = new Org(dto);

		MessageResult messageResult = new MessageResult();
		int i = 0;

		try {
			OrgExample example = new OrgExample();
			example.createCriteria().andOrgCodeEqualTo(org.getOrgCode());

			org.setUpdatedAt(new Date());
			i = orgMapper.updateByExampleSelective(org, example);
		} catch (DuplicateKeyException e) {
			messageResult.setResultMessage(ServiceCode.DICT_CODE_DUPLICATE);
			log.error(ServiceCode.DICT_CODE_DUPLICATE);
		} catch (Exception e) {
			messageResult.setResultMessage(ServiceCode.EXCEPTION_ERROR);
			log.error(e.getMessage(), e);
		}

		if (i > 0)
			messageResult.setResultMessage(ServiceCode.SUCCESS);

		return messageResult;
	}

	@Override
	public List<Org> queryOrg() {
		OrgExample example = new OrgExample();
		List<Org> orgList = orgMapper.selectByExample(example);
		
		if (CollectionUtils.isEmpty(orgList)) {
			return Collections.emptyList();
		}

		return orgList;
	}
}
